package com.song.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.request.ApBannerRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.song.module.admin.mapper.ApBannerMapper;
import org.springframework.beans.BeanUtils;
import com.song.module.admin.entity.ApBanner;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.song.module.admin.service.IApBannerService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 轮播业务层处理
 *
 * @author song
 * @date 2024-02-28
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApBannerServiceImpl implements IApBannerService {

    private final ApBannerMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApBanner add(ApBannerRequest request) {
        ApBanner entity = ApBanner.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public ApBanner getById(Long id) {
        return baseMapper.selectById(id);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ApBannerRequest request) {
        ApBanner entity = ApBanner.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }



    @Override
    public PageInfo<ApBanner> pageList(ApBannerRequest request) {
        Page<ApBanner> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<ApBanner> query = this.buildWrapper(request);
        IPage<ApBanner> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }

    @Override
    public List<ApBanner> list(ApBannerRequest request) {
        LambdaQueryWrapper<ApBanner> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }


    @Override
    public List<ApBanner> aplist(ApBannerRequest request) {
        LambdaQueryWrapper<ApBanner> query = this.buildWrapper(request);
        return baseMapper.selectList(query).stream().filter(item->item.getState()==0).collect(Collectors.toList());
    }
    private LambdaQueryWrapper<ApBanner> buildWrapper(ApBannerRequest param) {
        LambdaQueryWrapper<ApBanner> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(ApBanner::getName, param.getName());
        }
        return query;
    }

}
